package com.yingxue.lesson.service.impl;


import com.github.pagehelper.PageHelper;
import com.yingxue.lesson.entity.SysRotationChart;
import com.yingxue.lesson.exception.BusinessException;
import com.yingxue.lesson.exception.code.BaseResponseCode;
import com.yingxue.lesson.mapper.SysRotationChartMapper;
import com.yingxue.lesson.service.FileService;
import com.yingxue.lesson.service.RotationChartService;
import com.yingxue.lesson.utils.PageUtil;
import com.yingxue.lesson.vo.req.rotationVo.RotationChartDeleteReqVO;
import com.yingxue.lesson.vo.req.rotationVo.RotationChartReqAddVO;
import com.yingxue.lesson.vo.req.rotationVo.RotationChartUpdateReqVO;
import com.yingxue.lesson.vo.req.rotationVo.RotationReqVO;
import com.yingxue.lesson.vo.resp.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RotationChartServiceImpl implements RotationChartService {


    @Autowired
    private SysRotationChartMapper sysRotationChartMapper;
    @Autowired
    private FileService fileService;

    /**
     * 分页查询所有
     */
    @Override
    public PageVO<SysRotationChart> pageInfo(RotationReqVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<SysRotationChart> rotationCharts = sysRotationChartMapper.selectAll();
        return PageUtil.getPageVo(rotationCharts);
    }


    /**
     * 添加轮播图
     */
    @Override
    public void addRotationChart(RotationChartReqAddVO vo, String userId) {
        SysRotationChart sysRotationChart = new SysRotationChart();
        BeanUtils.copyProperties(vo,sysRotationChart);
        sysRotationChart.setCreateId(userId);
        sysRotationChart.setId(UUID.randomUUID().toString());
        sysRotationChart.setCreateTime(new Date());
        sysRotationChart.setCreateId(userId);
        int i = sysRotationChartMapper.insertSelective(sysRotationChart);
        if(i!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }


    /**
     * 更新轮播图
     */
    @Override
    public void updateRotationChart(RotationChartUpdateReqVO vo, String userId) {
        //1.查询
        SysRotationChart sysRotationChart = sysRotationChartMapper.selectByPrimaryKey(vo.getId());
        if(sysRotationChart==null){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        //2.若更新了图片，则删除之前的图片
        if (!sysRotationChart.getFileUrl().equals(vo.getFileUrl())){
            //首先要把文件按信息表的数据删除
            //就是把磁盘上的文件删除
            int i = fileService.deleteByFileUrl(sysRotationChart.getFileUrl());
        }
        //3.更新
        BeanUtils.copyProperties(vo,sysRotationChart);
        sysRotationChart.setUpdateId(userId);
        sysRotationChart.setUpdateTime(new Date());
        int i = sysRotationChartMapper.updateByPrimaryKeySelective(sysRotationChart);
        if(i!=1){
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    /**
     * 删除\批量删除
     */
    @Override
    public int deletedRotation(List<RotationChartDeleteReqVO> vos) {
        //1.删除轮播图数据库
        int i = sysRotationChartMapper.batchDeletedRotation(vos);
        //2.删除文件数据库，再删除磁盘文件
        for (RotationChartDeleteReqVO vo : vos) {
            fileService.deleteByFileUrl(vo.getFileUrl());
        }
        return i;
    }


    /**
     * 轮播图查询
     */
    @Override
    public List<SysRotationChart> selectAll() {
        return sysRotationChartMapper.selectAll();
    }
}
